<?php


namespace App\Dao;


use App\Model\AdminUser;

class AdminUserDao extends AbstractDao
{
    /**
     * @param int $id
     * @return int|mixed
     */
    public function delete(int $id)
    {
        return $this->where(['id' => $id])->delete();
    }

    public function where(array $where): \Hyperf\Database\Model\Builder
    {
        $builder = AdminUser::query();

        foreach ($where as $field => $value) {
            switch ($field) {
                case 'not_show_default':
                    $builder->where('mobile','<>',config('menus.default_admin_user.mobile'));
                    break;
                case 'keywords':
                    $builder
                        ->where('username', 'like', '%' . $value . '%')
                        ->orWhere('mobile', 'like', '%' . $value . '%');
                    break;
                case 'ids':
                    $builder->whereIn('id',$value);
                    break;
                default:
                    $builder->where($field,$value);
            }
        }

        return $builder;
    }


    /**
     * 获取所有用户id
     * @return array
     */
    public function getIds()
    {
        $result = $this->select([])->toArray();

        $ids = [];
        if (count($result) > 0) {
            $ids = array_column($result, 'id');
        }
        return $ids;
    }
}